/*     */ package com.newcrm.util;
/*     */ 
/*     */ import com.newcrm.db.CompanyExtManager;
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.UserVOIPManager;
/*     */ import com.newcrm.db.VOIPCallManager;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.net.MalformedURLException;
/*     */ import java.net.URL;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import net.sf.json.JSONArray;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.io.FileUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.jsoup.Connection;
/*     */ import org.jsoup.Jsoup;
/*     */ 
/*     */ public class VOIPUtil
/*     */ {
/*  33 */   private static Logger logger = Logger.getLogger(VOIPUtil.class.getName());
/*     */ 
/*     */   public static int loadCallLog(String userId, String pwd, Date startDate, Date endDate, int companyId, int page, boolean isSinglePage, String combox, String extendNo)
/*     */   {
/*     */     try
/*     */     {
/*  47 */       Connection.Response response = 
/*  48 */         Jsoup.connect("http://voip.qikebao.com/action?callback=jQuery1102031366848526522517_" + Util.random(13))
/*  49 */         .userAgent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB6.6; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET CLR 1.1.4322; .NET4.0C)")
/*  50 */         .data("action", "login")
/*  51 */         .data("data", 
/*  52 */         "{\"loginName\":\"" + 
/*  53 */         userId + 
/*  54 */         "\",\"password\":\"" + 
/*  55 */         pwd + 
/*  56 */         "\",\"remember\":true,\"type\":\"ucenter\"}")
/*  57 */         .timeout(10000).method(Connection.Method.POST).execute();
/*     */ 
/*  59 */       String body = response.body();
/*  60 */       int start = body.indexOf("(");
/*  61 */       int end = body.lastIndexOf(")");
/*  62 */       if (start <= 0) {
/*  63 */         logger.warn("login failed without start symbol");
/*  64 */         return 0;
/*     */       }
/*     */ 
/*  67 */       if (end < start) {
/*  68 */         logger.warn("login failed without end symbol");
/*  69 */         return 0;
/*     */       }
/*     */ 
/*  72 */       String json = body.substring(start + 1, end);
/*     */ 
/*  74 */       JSONObject jsonObject = JSONObject.fromObject(json);
/*  75 */       boolean flag = Util.getJSONBooleanValue(jsonObject, "success");
/*  76 */       if (!flag)
/*     */       {
/*  78 */         logger.warn("login failed success is not true");
/*  79 */         return 0;
/*     */       }
/*  81 */       String sessionId = Util.getJSONStringValue(jsonObject, "sessionId");
/*  82 */       if ((sessionId == null) || (sessionId.trim().length() == 0)) {
/*  83 */         logger.warn("login failed sessionid is null");
/*  84 */         return 0;
/*     */       }
/*     */ 
/*  88 */       response = Jsoup.connect("http://voip.qikebao.com/action?callback=jQuery1102031366848526522517_" + Util.random(13))
/*  89 */         .userAgent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB6.6; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET CLR 1.1.4322; .NET4.0C)")
/*  90 */         .data("action_id", "0." + Util.random(17))
/*  91 */         .data("sessionId", sessionId)
/*  92 */         .data("action", "app.getDicCache")
/*  93 */         .data("data", "{\"type\":\"agents\"}").timeout(10000)
/*  94 */         .method(Connection.Method.POST).execute();
/*     */ 
/*  96 */       body = response.body();
/*  97 */       start = body.indexOf("(");
/*  98 */       end = body.lastIndexOf(")");
/*  99 */       if (start <= 0) {
/* 100 */         logger.warn("get agent failed without start symbol");
/* 101 */         return 0;
/*     */       }
/*     */ 
/* 104 */       if (end < start) {
/* 105 */         logger.warn("get agent failed without end symbol");
/* 106 */         return 0;
/*     */       }
/*     */ 
/* 109 */       json = body.substring(start + 1, end);
/*     */ 
/* 111 */       jsonObject = JSONObject.fromObject(json);
/* 112 */       flag = jsonObject.getBoolean("success");
/* 113 */       if (!flag)
/*     */       {
/* 115 */         logger.warn("get agent info failed, success is not true");
/* 116 */         return 0;
/*     */       }
/* 118 */       JSONArray list = jsonObject.getJSONArray("data");
/* 119 */       if ((list == null) || (list.size() == 0)) {
/* 120 */         logger.warn("get agent info failed, data is null");
/* 121 */         return 0;
/*     */       }
/*     */ 
/* 124 */       String agentId = "";
/*     */ 
/* 126 */       java.sql.Connection conn = ConnectionManager.getInstance().getConnection();
/* 127 */       if (conn != null)
/*     */       {
/*     */         try
/*     */         {
/* 131 */           UserVOIPManager userVOIPManager = new UserVOIPManager();
/* 132 */           List voipNames = userVOIPManager
/* 133 */             .getUsersVOIPName(conn, companyId);
/*     */ 
/* 135 */           Map agents = new HashMap();
/* 136 */           for (int i = 0; i < list.size(); i++) {
/* 137 */             JSONObject item = list.getJSONObject(i);
/*     */ 
/* 140 */             if (extendNo.contains(item.getString("exten"))) {
/* 141 */               agentId = item.getString("_id");
/*     */             }
/* 143 */             agents.put(
/* 144 */               item.getString("_id"), 
/* 145 */               findUserName(item.getString("exten"), voipNames));
/*     */           }
/*     */ 
/* 149 */           int count = getCallLog(conn, agents, companyId, sessionId, 
/* 150 */             startDate, endDate, page, isSinglePage, combox, 
/* 151 */             agentId);
/*     */ 
/* 153 */           if (isSinglePage) {
/* 154 */             return count;
/*     */           }
/*     */ 
/* 158 */           VOIPCallManager manager = new VOIPCallManager();
/* 159 */           manager.addCallLog(conn, startDate, endDate, count, 
/* 160 */             companyId);
/*     */         } finally {
/*     */           try {
/* 163 */             DbUtils.close(conn);
/*     */           } catch (SQLException e) {
/* 165 */             logger.warn("close conn failed", e);
/*     */           }
/*     */         }
/*     */         try
/*     */         {
/* 163 */           DbUtils.close(conn);
/*     */         } catch (SQLException e) {
/* 165 */           logger.warn("close conn failed", e);
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (IOException e) {
/* 170 */       logger.warn("get voip call info failed", e);
/*     */     }
/* 172 */     return 1;
/*     */   }
/*     */ 
/*     */   private static int getCallLog(java.sql.Connection conn, Map<String, String> agents, int companyId, String sessionId, Date startDate, Date endDate, int page, boolean isSinglePage, String combox, String agentId)
/*     */   {
/* 191 */     if (conn == null) {
/* 192 */       return 0;
/*     */     }
/* 194 */     Date endDate2 = endDate;
/* 195 */     if (startDate == null)
/* 196 */       endDate2 = null;
/*     */     try
/*     */     {
/* 199 */       Connection.Response response = 
/* 200 */         Jsoup.connect("http://voip.qikebao.com/action?callback=jQuery1102031366848526522517_" + Util.random(13))
/* 201 */         .userAgent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB6.6; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET CLR 1.1.4322; .NET4.0C)")
/* 202 */         .data("action_id", "0." + Util.random(17))
/* 203 */         .data("sessionId", sessionId)
/* 204 */         .data("action", "app.fee.queryCallSheet2CC")
/* 205 */         .data("data", 
/* 206 */         "{\"combox\":\"" + 
/* 207 */         combox + 
/* 208 */         "\",\"CONNECT_TYPE\":\"\",\"STATUS\":\"dealing\",\"DISPOSAL_AGENT\":\"" + 
/* 209 */         agentId + "\",\"startOfferingTime\":\"" + 
/* 210 */         Util.date2Str3(startDate) + 
/* 211 */         "\",\"endOfferingTime\":\"" + 
/* 212 */         Util.date2Str3(endDate2) + 
/* 213 */         "\",\"page\":\"" + page + 
/* 214 */         "\",\"limit\":10}").timeout(10000)
/* 215 */         .method(Connection.Method.POST).execute();
/*     */ 
/* 217 */       String body = response.body();
/* 218 */       int start = body.indexOf("(");
/* 219 */       int end = body.lastIndexOf(")");
/* 220 */       if (start <= 0) {
/* 221 */         logger.warn("get call info failed without start symbol");
/* 222 */         return 0;
/*     */       }
/*     */ 
/* 225 */       if (end < start) {
/* 226 */         logger.warn("get call info failed without end symbol");
/* 227 */         return 0;
/*     */       }
/*     */ 
/* 230 */       String json = body.substring(start + 1, end);
/*     */ 
/* 232 */       JSONObject jsonObject = JSONObject.fromObject(json);
/* 233 */       boolean flag = Util.getJSONBooleanValue(jsonObject, "success");
/* 234 */       if (!flag)
/*     */       {
/* 236 */         logger.warn("get call info failed sucess is false");
/* 237 */         return 0;
/*     */       }
/*     */ 
/* 240 */       int count = jsonObject.getInt("count");
/* 241 */       JSONArray list = jsonObject.getJSONArray("list");
/*     */ 
/* 243 */       List baseParams = new ArrayList();
/*     */ 
/* 245 */       int num = 0;
/* 246 */       for (int i = 0; i < list.size(); i++) {
/* 247 */         JSONObject item = list.getJSONObject(i);
/* 248 */         List info = new ArrayList();
/*     */ 
/* 250 */         info.add(Util.getJSONStringValue(item, "CALL_ID"));
/*     */ 
/* 252 */         String callUserId = (String)agents.get(Util.getJSONStringValue(item, 
/* 253 */           "DISPOSAL_AGENT"));
/* 254 */         info.add(callUserId);
/*     */ 
/* 256 */         info.add(Util.getJSONStringValue(item, "CALLED_NO"));
/* 257 */         info.add(Integer.valueOf(1));
/*     */ 
/* 260 */         info.add(Util.getJSONStringValue(item, "BEGIN_TIME"));
/*     */ 
/* 262 */         info.add(Util.getJSONStringValue(item, "END_TIME"));
/*     */ 
/* 264 */         info.add(Integer.valueOf(Util.getJSONIntValue(item, "CALL_TIME_LENGTH")));
/*     */ 
/* 266 */         info.add(Util.getJSONStringValue(item, "FILE_SERVER") + "/" + 
/* 267 */           Util.getJSONStringValue(item, "RECORD_FILE_NAME"));
/* 268 */         info.add("");
/* 269 */         info.add(Integer.valueOf(companyId));
/* 270 */         info.add(callUserId);
/* 271 */         num++;
/* 272 */         baseParams.add(info.toArray(new Object[0]));
/*     */       }
/*     */ 
/* 275 */       VOIPCallManager manager = new VOIPCallManager();
/* 276 */       manager.add(conn, baseParams);
/*     */ 
/* 278 */       if (isSinglePage) {
/* 279 */         return count;
/*     */       }
/*     */ 
/* 283 */       if ((num == 0) || ((page - 1) * 10 + num >= count)) {
/* 284 */         return count;
/*     */       }
/*     */ 
/*     */       try
/*     */       {
/* 289 */         Thread.sleep(3000L);
/*     */       }
/*     */       catch (InterruptedException localInterruptedException) {
/*     */       }
/* 293 */       getCallLog(conn, agents, companyId, sessionId, startDate, endDate, 
/* 294 */         page + 1, isSinglePage, combox, agentId);
/*     */ 
/* 296 */       return count;
/*     */     } catch (IOException e) {
/* 298 */       logger.warn("get call info failed", e);
/*     */     }
/* 300 */     return 0;
/*     */   }
/*     */ 
/*     */   public static void getLogInfo(int companyId)
/*     */   {
/* 310 */     CompanyExtManager manager = new CompanyExtManager();
/* 311 */     String userName = manager.getFieldValue(companyId, 
/* 312 */       4);
/* 313 */     if ((userName == null) || (userName.trim().length() == 0)) {
/* 314 */       return;
/*     */     }
/* 316 */     String userPwd = manager.getFieldValue(companyId, 
/* 317 */       5);
/* 318 */     if ((userPwd == null) || (userPwd.trim().length() == 0)) {
/* 319 */       return;
/*     */     }
/*     */ 
/* 322 */     String serverIP = manager.getFieldValue(companyId, 
/* 323 */       6);
/* 324 */     if ((serverIP == null) || (serverIP.trim().length() == 0)) {
/* 325 */       return;
/*     */     }
/*     */ 
/* 328 */     VOIPCallManager voipCallManager = new VOIPCallManager();
/*     */ 
/* 331 */     if (serverIP.contains("ucpaas.qikebao.com"))
/*     */     {
/* 333 */       UCPAASUtil.loadCallLog(userName, "", 
/* 334 */         voipCallManager.getLastSynchronousTime(companyId), 
/* 335 */         new Date(), "", companyId, 1, false);
/*     */     }
/*     */     else
/*     */     {
/* 339 */       loadCallLog(userName, userPwd, 
/* 340 */         voipCallManager.getLastSynchronousTime(companyId), 
/* 341 */         new Date(), companyId, 1, false, "", "");
/*     */     }
/*     */   }
/*     */ 
/*     */   public static int downloadFile(String filePath)
/*     */   {
/* 353 */     VOIPCallManager manager = new VOIPCallManager();
/* 354 */     List items = manager.getDownloadUrl();
/* 355 */     if ((items == null) || (items.size() == 0)) {
/* 356 */       return 0;
/*     */     }
/*     */ 
        for (Object itemO : items) {
            Map item = (Map)itemO;
/* 360 */       int id = ((Integer)item.get("VOIPId")).intValue();
/* 361 */       String url = (String)item.get("FileUrl");
/* 362 */       if ((url == null) || (url.trim().length() == 0) || (url.startsWith("*"))) {
/* 363 */         manager.updateFilePath(id, "*");
/*     */       }
/*     */       else {
/* 366 */         String telephone = (String)item.get("Telephone");
/*     */ 
/* 368 */         File folder = new File(filePath + File.separator + telephone);
/* 369 */         if ((!folder.exists()) && (!folder.isDirectory())) {
/* 370 */           boolean flag = folder.mkdir();
/* 371 */           if (!flag);
/*     */         }
/*     */         else {
/* 375 */           String fileName = MD5.getMD5(url);
/* 376 */           if (url.contains("ucpaas"))
/* 377 */             fileName = fileName + ".wav";
/*     */           else {
/* 379 */             fileName = fileName + ".mp3";
/*     */           }
/* 381 */           File file = new File(folder, fileName);
/* 382 */           if (file.exists())
/* 383 */             file.delete();
/*     */           try
/*     */           {
/* 386 */             FileUtils.copyURLToFile(new URL(url), file);
/*     */ 
/* 388 */             manager.updateFilePath(id, fileName);
/*     */           } catch (MalformedURLException e) {
/* 390 */             logger.warn("download recording file failed", e);
/*     */           } catch (IOException e) {
/* 392 */             logger.warn("download recording file failed", e);
/*     */           }
/*     */ 
/*     */           try
/*     */           {
/* 397 */             Thread.sleep(1000L);
/*     */           } catch (InterruptedException e) {
/* 399 */             e.printStackTrace();
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 402 */     return items.size();
/*     */   }
/*     */ 
/*     */   private static String findUserName(String exten, List<Map<String, Object>> voipNames)
/*     */   {
/* 414 */     if (voipNames == null) {
/* 415 */       return "";
/*     */     }
/*     */ 
/* 418 */     for (Map item : voipNames) {
/* 419 */       String name = (String)item.get("UserName");
/* 420 */       if (name.contains(exten)) {
/* 421 */         return (String)item.get("UserId");
/*     */       }
/*     */     }
/* 424 */     return "";
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.util.VOIPUtil
 * JD-Core Version:    0.6.1
 */